from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet
from goods.models import SpecificationOption, SPUSpecification
from meiduo_admin.good import specs_options_serializers
from meiduo_admin.my_paginate import MyPageNumberPagination

#1,spec option管理
class SpecsOptionViewSet(ModelViewSet):
    pagination_class = MyPageNumberPagination
    serializer_class = specs_options_serializers.SpecsOptionSerializer
    queryset = SpecificationOption.objects.all()

#2,spec 信息获取
class SpecSimpleView(ListAPIView):
    serializer_class = specs_options_serializers.SpecSimpleSerializer
    # queryset = SPUSpecification.objects.all()

    #1,重写get_queryset方法,目的在前端显示数据的时候,方便查看
    def get_queryset(self):
        #1,获取数据源
        queryset = SPUSpecification.objects.all()

        #2,给数据源name增加spu.name
        for spec in queryset:
            spec.name = "%s - %s"%(spec.spu.name,spec.name)

        #3,返回数据源
        return queryset